'use strict';
const {
  Model
} = require('sequelize');
// 时间格式化工具
const moment = require('moment')
// 设置moment为中文格式
moment.locale('zh-cn')
module.exports = (sequelize, DataTypes) => {
  class Chapter extends Model { //章节表，一个课程对应多个章节
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      models.Chapter.belongsTo(models.Course,{as:'course'})
    }
  }
  Chapter.init({
    courseId: {
      type:DataTypes.INTEGER,
      allowNull:false,
      validate:{
        notNull:{msg:'课程Id不能为空'},
        notEmpty:{msg:'课程Id必须存在'},

      }
    },
    title: DataTypes.STRING,
    content: DataTypes.TEXT,
    video: DataTypes.STRING,
    rank: DataTypes.INTEGER,
    createdAt:{
      type:DataTypes.DATE,
      get(){
        return moment(this.getDataValue('createdAt')).format('LL')
      }
    },
    updatedAt:{
      type:DataTypes.DATE,
      get(){
        return moment(this.getDataValue('updatedAt')).format('LL')
      }
    },
  }, {
    sequelize,
    modelName: 'Chapter',
  });
  return Chapter;
};